class CameraInterpreter implements IInterpreter {
  
  Detector bd;
  Capture frame;
  PImage img;
  boolean hasFoundBlobs;
  int blobsNumber;
  ArrayList<PVector[]> blobList;
  camera_shapePhysics app;
  
  int camera_height;
  int camera_width;
  
  CameraInterpreter(camera_shapePhysics app_pointer) {
    app = app_pointer;
    blobList = new ArrayList<PVector[]>();
    camera_height = 576;
    camera_width = 704;
    frame = new Capture(app, camera_width, camera_height);
    bd = new Detector( app, 0, 0, frame.width, frame.height, 255 );
    img = createImage(camera_width, camera_height, RGB);
  }
  
  void init() {
    //we look for black blobs so last param value is 0
    if(blobList.size() > 0)
      blobList.clear();
    blobsNumber = 0;
    hasFoundBlobs = false; 
    println("CameraInterpreter.init()");
  }
  
  void findBlobs() {
    if(frame.available()){
      frame.read();
      //save a copy of the frame to send to the output video stream
      img.copy(frame, 0, 0, camera_width, camera_height, 0, 0, camera_width, camera_height);
    }
    // Prepare the Camera Image Data
    frame.filter(GRAY);
    frame.filter(INVERT);
    frame.filter(BLUR);
    frame.filter(THRESHOLD);
    // Analyzing the camera data
    bd.imageFindBlobs(frame);
    bd.loadBlobsFeatures();
     
    image(img, 0, 0);
    
    int minimumWeight = 1000;
    int thickness = 3;
    color contoursColor = color(255, 0, 0);
    color boundingBoxColor = color(0, 255, 0);
    color selectBoxColor = color(255, 255, 0);
    color selectContoursColor = color(0, 255, 255);
    
    bd.drawContours(contoursColor, thickness);
    //bd.drawSelectContours(minimumWeight, selectContoursColor, thickness);
    //bd.drawBox(boundingBoxColor, thickness);
    //bd.drawSelectBox(minimumWeight, selectBoxColor, thickness);
    blobsNumber = bd.getBlobsNumber();
    println("CameraInterpreter: found " + blobsNumber + " blobs");
    app.labelBlobs.setValue("CameraInterpreter: found " + blobsNumber + " blobs");
    //useBlobData(); // call this function manually by pressing the bang button
  }
  
  boolean hasFoundBlobs(){
    return hasFoundBlobs;
  }
  
  ArrayList<PVector[]> getBlobList() {
    return blobList;
  }
  
  // take the blob data manually
  void useBlobData() {
    println("cameraInterpreter.useBlobData()");
    if(!hasFoundBlobs) {
      //For each blob
      blobsNumber = bd.getBlobsNumber();
      println("CameraInterpreter.useBlobData: found " + blobsNumber + " blobs");
      int blobWeight = 0;
      for(int i = 0; i < bd.getBlobsNumber(); i++){
        blobList.add(bd.getEdgePoints(i)); 
        println("CameraInterpreter: add blob to the list");
        hasFoundBlobs = true;   
      }
    }
  }
}
